﻿package com.tiankong.android.wallpaper;

import com.tiankong.android.wallpaper.bean.PhoneInfo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.format.Time;

public class PhoneBroadcastReceiver extends BroadcastReceiver {

	private Time date = null;

	public PhoneBroadcastReceiver() {
		super();
		
		date = new Time();
		date.setToNow();
		PhoneInfo.year=date.year;
		PhoneInfo.month=(date.month + 1);
		PhoneInfo.monthDay=date.monthDay;
		PhoneInfo.weekDay=date.weekDay;
		PhoneInfo.hour=date.hour;
		PhoneInfo.min=date.minute;
		
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		getPhoneInfo(context, intent);
	}
	

	private void getPhoneInfo(Context context, Intent intent) {
		
		if (intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {
			PhoneInfo.batteryPercent=(intent.getExtras().getInt("level") * 100
					/ intent.getExtras().getInt("scale"));
			if (intent.getExtras().getInt("plugged") != 0) {
				PhoneInfo.hasPower=true;
			} else {
				PhoneInfo.hasPower=false;
			}
		} else if (intent.getAction().equals(Intent.ACTION_BATTERY_OKAY)) {
			PhoneInfo.batteryLow=false;
		} else if (intent.getAction().equals(Intent.ACTION_BATTERY_LOW)) {
			PhoneInfo.batteryLow=true;
		} else if (intent.getAction().equals(Intent.ACTION_TIME_TICK)) {
			date.setToNow();
			PhoneInfo.hour=date.hour;
			PhoneInfo.min=date.minute;
		} else if (intent.getAction().equals(Intent.ACTION_DATE_CHANGED)) {
			date.setToNow();
			PhoneInfo.year=date.year;
			PhoneInfo.month=(date.month + 1);
			PhoneInfo.monthDay=date.monthDay;
			PhoneInfo.weekDay=date.weekDay;
		}
	}
	
}
